﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccessLibrary;

namespace BusinessLayer
{
    public class ClsSalaryHead
    {
        public string SalaryHead { get; set; }
        public int SalaryHeadID { get; set; }
        public bool DeductSalaryIfAbsent { get; set; }
        public bool Debit { get; set; }


        public DataTable GetAllSalaryHead()
        {
            using (OleDataClient data = new OleDataClient())
            {
                return data.GetDataTable("Select * from SalaryHeads", "SalaryHeads");
            }
        }
        public void GetSalaryHead(int intSalaryHeadID)
        {
            using (OleDataClient data = new OleDataClient())
            {
                DataTable dt = data.GetDataTable("Select * from SalaryHeads where SalaryHeadID=" + intSalaryHeadID , "SalaryHeads");
                if (dt.Rows.Count > 0)
                {
                    SalaryHeadID = Convert.ToInt32(dt.Rows[0]["SalaryHeadID"]);
                    SalaryHead = dt.Rows[0]["SalaryHead"].ToString();
                    DeductSalaryIfAbsent = Convert.ToBoolean(dt.Rows[0]["DeductSalaryIfAbsent"]);
                    Debit = Convert.ToBoolean(dt.Rows[0]["Debit"]);
                }
            }
        }
        public bool SaveSalaryHead(string KeyForSave)
        {
            using (OleDataClient data = new OleDataClient())
            {
                if (KeyForSave == "add")
                {
                    string count = data.GetValue("select count(*) from SalaryHeads where SalaryHead='" + SalaryHead + "'");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("Salary Head already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Insert into SalaryHeads (SalaryHead,DeductSalaryIfAbsent,Debit) values('" + SalaryHead + "'," + DeductSalaryIfAbsent + "," + Debit + " )");
                    }

                }
                else
                {
                    data.ExecuteNonQuery("Update SalaryHeads set SalaryHead = '" + SalaryHead + "', DeductSalaryIfAbsent =  " + DeductSalaryIfAbsent +   ", Debit= " + Debit + " where SalaryHeadID=" + SalaryHeadID.ToString());
                                     
                }
            }
            return true;
        }
        public void DeleteSalaryHead()
        {
            using (OleDataClient data = new OleDataClient())
            {
                data.ExecuteNonQuery("Delete from  SalaryHeads where SalaryHeadID=" + SalaryHeadID.ToString());
            }
        }
    }
}
